<?php
namespace app\logic\yuql;



use app\api\model\yuql\Browse;
use app\api\model\yuql\Collection;
use app\api\model\yuql\Purchase;
use app\api\model\yuql\Search;
use app\api\model\yuql\Seckill;
use app\services\yuql\RedisServices;
use think\route\dispatch\Controller;

class HistoryLogic
{

    public static function getUserBehavior($uid)
    {
        // 获取用户浏览历史、搜索记录、购买记录、收藏记录等数据
        $searchHistory = Search::HistoryS(['uid'=>$uid]);       // 获取用户搜索记录
        $browseHistory = Browse::HistoryS(['uid'=>$uid]);       // 获取用户浏览历史
        $purchaseHistory = Purchase::HistoryS(['uid'=>$uid]);         // 获取用户购买记录
        $collectionHistory = Collection::HistoryS(['uid'=>$uid]);       // 获取用户收藏记录
        $result = HistoryLogic::getProductRecommendation($searchHistory,$browseHistory,$purchaseHistory,$collectionHistory);
        return $result;
    }


    public static function getProductRecommendation($searchHistory,$browseHistory,$purchaseHistory,$collectionHistory)
    {
        $key = '2008a-History';
        $redis = RedisServices::getInstance()->getRedis();
        $redis->select(6);
        // 根据用户搜索记录推荐商品
        foreach ($searchHistory as $search_v) {
            $searchProduct = $search_v['search_history'];
            $searchHistoryGoods = Seckill::userSearchGoods($searchProduct);
            foreach ($searchHistoryGoods as $val){
                $redis->sAdd($key,$val['goods_id']);
            }
        }
        foreach ($browseHistory as $browse_v) {
            $redis->sAdd($key,$browse_v['browse_history']);
        }
        foreach ($purchaseHistory as $purchase_v) {
            $redis->sAdd($key,$purchase_v['purchase_history']);
        }
        foreach ($collectionHistory as $collection_v) {
            $redis->sAdd($key,$collection_v['collection_history']);
        }
        $productRecommendation = $redis->sMembers($key);
        foreach ($productRecommendation as $v){
            $goodsList = Seckill::RecommendGoodsList(['goods_id' => $v]);
            $recommendGoodsAll[] = $goodsList;
        }
        return $recommendGoodsAll;
    }
}